use serde::{Deserialize, Serialize};
use crate::models::state::{CallRecord, CallType, CallStatus};

/// 数据库中的通话记录结构（实体/持久化模型）
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DbCallRecord {
    pub id: String,
    pub target: String,
    pub target_display: String,
    pub call_type: String,
    pub status: String,
    pub timestamp: i64,
    pub duration: Option<i64>,
}

impl From<CallRecord> for DbCallRecord {
    fn from(record: CallRecord) -> Self {
        Self {
            id: record.id,
            target: record.target,
            target_display: record.target_display,
            call_type: match record.call_type {
                CallType::Outgoing => "Outgoing".to_string(),
                CallType::Incoming => "Incoming".to_string(),
                CallType::Missed => "Missed".to_string(),
            },
            status: match record.status {
                CallStatus::Connected => "Connected".to_string(),
                CallStatus::Failed => "Failed".to_string(),
                CallStatus::Cancelled => "Cancelled".to_string(),
                CallStatus::NoAnswer => "NoAnswer".to_string(),
            },
            timestamp: record.timestamp,
            duration: record.duration.map(|d| d as i64),
        }
    }
}

impl From<DbCallRecord> for CallRecord {
    fn from(record: DbCallRecord) -> Self {
        Self {
            id: record.id,
            target: record.target,
            target_display: record.target_display,
            call_type: match record.call_type.as_str() {
                "Outgoing" => CallType::Outgoing,
                "Incoming" => CallType::Incoming,
                "Missed" => CallType::Missed,
                _ => CallType::Outgoing,
            },
            status: match record.status.as_str() {
                "Connected" => CallStatus::Connected,
                "Failed" => CallStatus::Failed,
                "Cancelled" => CallStatus::Cancelled,
                "NoAnswer" => CallStatus::NoAnswer,
                _ => CallStatus::Cancelled,
            },
            timestamp: record.timestamp,
            duration: record.duration.map(|d| d as u32),
        }
    }
}

